package com.example.tests;

import static org.testng.AssertJUnit.assertTrue;

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

import com.example.fw.AccountHelper;
import com.example.fw.AdminHelper;
import com.example.fw.HibernateHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SignUpTest extends TestBase {

	User user = new User().setLogin("TestUser").setPassword("123456").setEmail("TestUser@localhost.localdomain");
	private JamesHelper james;
	private AccountHelper accHelper;
	private HibernateHelper db;
	private AdminHelper admin;
	
	@BeforeClass
	public void createMailUser() {
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		admin = app.getAdminHelper();
		db = app.getHebirnateHelper();
		if (!james.doesUserExist(user.login)) {
			james.createUser(user.login, user.password);
		}
	}
	
	@AfterClass
	public void deleteMailAndMantisUsers() {
		if (james.doesUserExist(user.login)) {
			james.deleteUser(user.login);
		}
	}
	
	@Test
	public void newUserShouldSignup() throws Exception {
		//admin.deleteUserIfExists(user);
		if (db.userExist(user)) {
			db.deleteUser(user);
		}
		accHelper.signup(user);
		assertTrue(db.userExist(user));
		accHelper.login(user);
		assertThat(accHelper.loggedUser(), equalTo(user.login));
	}
	
	//@Test
	public void existedUserShouldNoSignup() {
		try {
			accHelper.signup(user);
		} catch (Exception e) {
			assertThat(e.getMessage(), containsString("That username is already being used"));
			return;
		}
		Assert.fail("Exception expected");
	}
}
